package com.lzh.eduservice.controller;


import com.lzh.commonutils.R;
import com.lzh.eduservice.client.VodClient;
import com.lzh.eduservice.entity.EduVideo;
import com.lzh.eduservice.service.EduVideoService;
import com.lzh.oss.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author lzh
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/eduService/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    //注入VodClient
    @Autowired
    private VodClient vodClient;

    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //根据小节id删除小节
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable String id) {
        //先根据小节id得到视频id
        EduVideo byId = eduVideoService.getById(id);
        String videoSourceId = byId.getVideoSourceId();
        //先判断表中的视频id是否为空
        if (!StringUtils.isEmpty(videoSourceId)) {
            //服务调用，删除小节的时候删除视频
            R result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001,"删除视频失败，熔断器。。。");
            }

        }
        eduVideoService.removeById(id);
        return R.ok();
    }

    @PostMapping("/updateVideo")
    public R updateById(@PathVariable EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }
}

